package com.pms.privilege.func.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.pms.privilege.func.model.FunctionModel;
import com.pms.privilege.func.service.FunctionService;
import com.pms.utils.Constant;
import com.pms.utils.CookieManage;

@Controller
public class FunctionController {
	@Autowired
	private FunctionService fs;
	
	/**
	 * 请求功能模块首页信息
	 * @param req
	 * @param mm
	 * @return
	 */
	@RequestMapping(value="funindex.pms")
	public String index(HttpServletRequest req,ModelMap mm){
		CookieManage cm = new CookieManage();
		Map<String, Cookie> map = new HashMap<String, Cookie>();
		map = cm.getCookies(req);
		if (map!=null&&map.size()>0&&map.containsKey("permission")) {
			String permission = map.get("permission").getValue();
			if (permission != null && "yes".equals(permission)) {
				List<FunctionModel> list=new ArrayList<FunctionModel>();
				list=fs.functionListService(0, 10);
				if(list.size()>0){
					mm.addAttribute("fnlist", list);
				}
				return "func/funcindex";
			}
		}
		return "redirect:login.pms";
		
	}
	
	/**
	 * 新增功能模块
	 * 
	 * @param req
	 */
	@RequestMapping(value = "/funcinsert.pms", method = RequestMethod.POST)
	public void userInsert(HttpServletRequest req, HttpServletResponse resp) {
		try {
			String func_name = req.getParameter("func_name");
			String func_link=req.getParameter("func_link");
			String valid = req.getParameter("func_valid");
			JSONObject json = new JSONObject();
			if(func_name!=null&&func_link!=null&valid!=null){
				FunctionModel fm=new FunctionModel();
				fm.setFunc_name(func_name);
				fm.setFunc_link(func_link);
				if(valid.equals("yes")){
					fm.setFunc_valid(Constant.valid.yes);
				}else{
					fm.setFunc_valid(Constant.valid.no);
				}
				json.put("result", fs.insertFunctionService(fm));
				json.put("msg", "OK");
			}else{
				json.put("result", "-1");
				json.put("msg", "OK");
			}
			resp.setContentType("application/json; charset=UTF-8");
			resp.getWriter().print(json);
			resp.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 修改功能模块
	 * 
	 * @param req
	 */
	@RequestMapping(value = "/funcupdate.pms", method = RequestMethod.POST)
	public void userUpdate(HttpServletRequest req, HttpServletResponse resp) {
		try {
			String func_id=req.getParameter("func_id");
			String func_name = req.getParameter("func_name");
			String func_link=req.getParameter("func_link");
			String valid = req.getParameter("func_valid");
			JSONObject json = new JSONObject();
			if(func_id!=null&&func_name!=null&&func_link!=null&valid!=null){
				FunctionModel fm=new FunctionModel();
				fm.setFunc_id(Long.parseLong(func_id));
				fm.setFunc_name(func_name);
				fm.setFunc_link(func_link);
				if(valid.equals("yes")){
					fm.setFunc_valid(Constant.valid.yes);
				}else{
					fm.setFunc_valid(Constant.valid.no);
				}
				json.put("result", fs.updateFunctionService(fm));
				json.put("msg", "OK");
			}else{
				json.put("result", "-1");
				json.put("msg", "OK");
			}
			resp.setContentType("application/json; charset=UTF-8");
			resp.getWriter().print(json);
			resp.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除功能模块
	 * 
	 * @param req
	 */
	@RequestMapping(value = "/funcdelete.pms", method = RequestMethod.POST)
	public void userDelete(String func_id, HttpServletResponse resp) {
		try {
			JSONObject json = new JSONObject();
			int result = fs.deleteFunctionService(Long.parseLong(func_id));
			if (result != -1) {
				json.put("result", result);
				json.put("msg", "OK");
			} else {
				json.put("result", result);
				json.put("msg", "OK");
			}
			resp.setContentType("application/json; charset=UTF-8");
			resp.getWriter().print(json);
			resp.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 检索所有功能模块
	 * @param req
	 * @param resp 返回json列表
	 */
	@RequestMapping(value="funcinfo.pms",method=RequestMethod.POST)
	public void funcInfoList(HttpServletRequest req,HttpServletResponse resp){
		
		try {
			List<FunctionModel> fnlist = fs.functionListService(0, 10);
			JSONArray funcArray=new JSONArray();
			for(FunctionModel fm:fnlist){
				JSONObject json=new JSONObject(fm);
				funcArray.put(json);
			}
			resp.setContentType("application/json; charset=UTF-8");
			resp.getWriter().print(funcArray);
			resp.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 检索所有有效功能模块
	 * @param req
	 * @param resp 返回json列表
	 */
	@RequestMapping(value="funcinfovalid.pms",method=RequestMethod.POST)
	public void funcValidInfoList(HttpServletRequest req,HttpServletResponse resp){
		
		try {
			List<FunctionModel> fnlist = fs.functionListService(0, 10);
			JSONArray funcArray=new JSONArray();
			for(FunctionModel fm:fnlist){
				JSONObject json=new JSONObject(fm);
				funcArray.put(json);
			}
			resp.setContentType("application/json; charset=UTF-8");
			resp.getWriter().print(funcArray);
			resp.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	

}
